/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SpringTAI.web;

/**
 *
 * @author Krzysztof Nyznar
 */
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import SpringTAI.service.PersonManager;
import SpringTAI.service.AddPerson;
import SpringTAI.domain.Person;

public class AddPersonController extends SimpleFormController {

    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());

    private PersonManager personManager;

    public ModelAndView onSubmit(Object command)
            throws ServletException {

        Person person = new Person();
        person.setName(((AddPerson) command).getName());
        person.setSurname(((AddPerson) command).getSurname());
        person.setAddress(((AddPerson) command).getAddress());
        
        
        personManager.addPerson(person);

        return new ModelAndView(new RedirectView(getSuccessView()));
    }

    protected Object formBackingObject(HttpServletRequest request) throws ServletException {
        AddPerson ap = new AddPerson();
        ap.setName("imie");
        ap.setSurname("nazwisko");
        ap.setAddress("adres");
        return ap;
    }

    public void setPersonManager(PersonManager personManager) {
        this.personManager = personManager;
    }

    public PersonManager getPersonManager() {
        return personManager;
    }

}